package fem2.strategies;

import inf.jlinalg.ArrayVector;
import fem2.Model;
import fem2.Observer;
import fem2.jlinalg.Solver;
import fem2.observer.XfemManager;

public class NonAdaptiveNewtonRaphsonStrategyForXfem extends NonAdaptiveNewtonRaphsonStrategy {

	public NonAdaptiveNewtonRaphsonStrategyForXfem(Model m, int nt) {
		super(m, nt);
	}

	@Override
	public void started(Solver leftHandSideSolver, ArrayVector rightHandSide, ArrayVector u) {
		/*
		 * initialize model (enumeration)
		 */
		super.started(leftHandSideSolver, rightHandSide, u);
	}

	@Override
	public void fireTimeStepStarted() {
		for (int i = 0; i < observers.size(); i++) {
			Observer o = observers.get(i);
			if (!(o instanceof XfemManager)) {
				o.fireTimeStepStarted();
			}
		}
	}

	@Override
	protected boolean solveOneStep(int i, Solver solver, ArrayVector rhs, ArrayVector u) {

		/*
		 * trigger Xfem manager to enrich the model
		 */
		for (Observer o : observers) {
			if (o instanceof XfemManager) {
				o.fireTimeStepStarted();
			}
		}

		/*
		 * initialize solver
		 */
		initializeSolver(solver);

		/*
		 * initialize auxiliary vector
		 */
		int n = model.getSize();
		r.setSize(n);
		u.setSize(n);
		rhs.setSize(n);
		uHat = new ArrayVector(n);

		return super.solveOneStep(i, solver, rhs, u);
	}

}
